home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8712 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  58 lines

  1. Path: thor.tu.hac.com!collins
  2. From: collins@thor.tu.hac.com (Ron Collins)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Why doesn't this work?
  5. Date: 6 Mar 1996 00:12:33 GMT
  6. Organization: Advanced Depot Systems
  7. Message-ID: <4hil9h$hh5@hacgate2.hac.com>
  8. References: <1996Mar4.161412.137442@forest>
  9. NNTP-Posting-Host: thor.tu.hac.com
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. ebromber@forest.drew.edu wrote:
  13. : Does anyone know why this password program doesn't work properly? And if 
  14. : you do know the problem how can I fix it? It rejects every password including 
  15. : the real password. The program was compiled using a MS-DOS compiler.
  16. : Thanks in advance.
  17. : ebromber@drew.edu
  18.  
  19. : main();
  20. : {    char real[4];
  21. :     char pass[100];
  22. :     int count=0;
  23. :     int i, error;
  24. :     char c;
  25. :     real[0]='j';real[1]='e';real[2]='r';real[3]='k';
  26. :     printf("PASSWORD: ");
  27. :     fflush(stdout);
  28. :     while (c=getch() !='\n')
  29. :     {     count++;
  30. :         pass[count]=c;
  31. :         putch('*');
  32. :     }
  33.  
  34. Change the above to look something like:
  35.  
  36.     while ((c = getch()) != '\n')
  37.     {
  38.         pass[count++] = c;
  39.         putch('*');
  40.     };
  41.  
  42. That is, increment "count"  _after_ assigning the character "c" to "pass".
  43.  
  44. :     if (count!=4) { printf("\nWRONG PASSWORD\n"); main();}
  45. :     error=0;
  46. :     for (i=0; i<4; i++)
  47. :         if (real[i]=pass[i]) error++;
  48. :     if (error>0) {printf("\nWRONG PASSWORD\n"); main();}
  49. : }
  50.  
  51.  
  52. By incrementing "count" before the assignment, you started saving char "c"
  53. in "pass[1]", not in "pass[0]", so your comparison is offset by 1 character
  54. position.
  55.  
  56.             -- collins --
  57.  
  58.